//
//  UrbanRecordingViewController.m
//  UrbanManagement
//
//  Created by Can on 18/4/12.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanRecordingViewController.h"
#import "UrbanHomeTableViewCell.h"
#import "UrbanWhereViewController.h"
#import "UrbanPhotoViewController.h"
#import "UrbanPrinterViewController.h"

@interface UrbanRecordingViewController ()<UITableViewDataSource,UITableViewDelegate,UrbanHomeDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *recordList;
@property (nonatomic, assign) NSInteger pageNumber;

@end

@implementation UrbanRecordingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleWithTitle:@"违法记录"];
    
    _recordList = [NSMutableArray new];
    
    _pageNumber = 1;
    
    [self getRecordList];
    
    [self creatView];
}

- (void)creatView{
    CGFloat height = NavBarHeight;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(10*screenW_6, 0, SCREENW - 20*screenW_6, SCREENH - height) style:UITableViewStylePlain];
    _tableView.backgroundColor = Color(247, 247, 247);
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = 0;
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    _tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
}

- (void)refresh
{
    _pageNumber = 1;
    [_recordList removeAllObjects];
    [self getRecordList];
}

//上拉加载更多
-(void)footerRefresh
{
    _pageNumber++;

    [self getRecordList];
}

#pragma mark - tabledelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _recordList.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithHexString:@"#f8f8f8"];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"HomeCell";
    UrbanHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UrbanHomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.delegate = self;
    
    [cell loadData:_recordList[indexPath.section] number:indexPath.section];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UrbanHomeTableViewCell cellHeight:_recordList[indexPath.section] number:indexPath.section];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

- (void)homeWhere:(NSInteger)num
{
    UrbanWhereViewController *where = [UrbanWhereViewController new];
    where.mapType = 0;
    where.latitude = [_recordList[num] objectForKey:@"latitude"];
    where.longitude = [_recordList[num] objectForKey:@"longitude"];
    [self.navigationController pushViewController:where animated:YES];
}

- (void)homePhone:(NSInteger)num
{
    UrbanPhotoViewController *photoVC = [UrbanPhotoViewController new];
    photoVC.imageOne = [_recordList[num] objectForKey:@"image1"];
    photoVC.imageTwo = [_recordList[num] objectForKey:@"image2"];
    [self.navigationController pushViewController:photoVC animated:YES];
}

- (void)printer:(NSInteger)num
{
    UrbanPrinterViewController *printer = [UrbanPrinterViewController new];
    
    printer.orderNumber = [_recordList[num] objectForKey:@"orderNumber"];
    printer.plateNumber = [_recordList[num] objectForKey:@"carNumber"];
    if ([[_recordList[num] objectForKey:@"carType"] intValue] == 1) {
        printer.carType = @"小车";
    }else{
        printer.carType = @"大车";
    }
    printer.numPlateColor = [_recordList[num] objectForKey:@"carNumColour"];
    printer.parkTime = [_recordList[num] objectForKey:@"carParkTime"];
    printer.address = [NSString stringWithFormat:@"%@%@",[_recordList[num] objectForKey:@"carAddress"],[_recordList[num] objectForKey:@"addressDetails"]];
    
    [self.navigationController pushViewController:printer animated:YES];
}

- (void)getRecordList
{
    [HudHelper showHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSMutableDictionary *dic = [NSMutableDictionary new];
        [dic setValue:[Storage getToken] forKey:@"user.id"];
        [dic setValue:[NSString stringWithFormat:@"%ld",(long)self->_pageNumber] forKey:@"pageNo"];
        [dic setValue:@"10" forKey:@"pageSize"];
        
        [HttpRequest PostrequestUrl:@"jeesite/appInterface/ZfOrderInfo/queryRecord" Dic:dic completion:^(NSDictionary *responseDic){
//            NSLog(@"responseDic:%@",responseDic);
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {//请求成功
                    [self->_recordList addObjectsFromArray:[[responseDic objectForKey:@"data"] objectForKey:@"list"]];
                    
                    if([[[responseDic objectForKey:@"data"] objectForKey:@"count"] floatValue]/10 <= self->_pageNumber){
                        self.tableView.mj_footer.hidden = YES;
                    }else{
                        self.tableView.mj_footer.hidden = NO;
                    }
                    [self->_tableView reloadData];
                }
                
                if ([self->_tableView.mj_header isRefreshing]) {
                    [self->_tableView.mj_header endRefreshing];
                }
                if ([self->_tableView.mj_footer isRefreshing]) {

                    [self->_tableView.mj_footer endRefreshing];
                }
            });
        }];
    });
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
